<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.interfaces.cese.duss.ceseutil.WorkflowCreateHandler" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="rs1" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>
<%
    //触发消息提醒流程
    bb.writeLog( "------------------getNewsAlter_Ajax-------------" );
    String xmmc = Util.null2String( request.getParameter( "xmmc" ) );//项目名称
    String lc = Util.null2String( request.getParameter( "lc" ) );//轮次
    JSONObject jsonData = new JSONObject();
    User user = HrmUserVarify.getUser( request, response );
    String userid = user.getUID() + "";
    Map<String, String> requestMainMap = new HashMap<>();
    WorkflowCreateHandler createHandler = new WorkflowCreateHandler();
    String IsNextFlow = "1";
    String workflowId = "38021";
    String link = "http://192.168.1.144/dist/#/opening?id=" + xmmc + "";
    String requestName = "消息提醒-" + DateUtil.getCurrentDate();
    //内部人员
    String sql1 = "select concat(concat(concat(xxtxr,','||cgy) ,','||jdr) , ','||jzr) from formtable_main_702 where zblx=? and lc=? ";
    rs.executeQuery( sql1, xmmc, lc );
    rs.writeLog(sql1, String.join( ",",xmmc, lc));
    rs.next();
    String innerRy = Util.null2String( rs.getString( 1 ) );
    innerRy =  equestStr(innerRy);
    bb.writeLog( "innerRy=" + innerRy );
    //供应商
    String sql = "select wm_concat(gysmc) from formtable_main_674 where lc=? and xmmc=? and  requestid is null";
    rs1.executeQuery( sql,lc,xmmc );
    rs1.writeLog(sql, String.join( ",",xmmc, lc));
    rs1.next();
    String gysmc = Util.null2String( rs1.getString( 1 ) );
    gysmc = equestStr(gysmc);
    bb.writeLog( "gysmc=" + gysmc );
    String gysmcs = getUserid( gysmc );
    bb.writeLog( "gysmcs=" + gysmcs );
    String str = "";
    if("".equals( innerRy ) && !"".equals( gysmcs )){
        str = gysmcs;
    }else if(!"".equals( innerRy ) && "".equals( gysmcs )){
        str = innerRy;
    }else {
        str = innerRy + "," + gysmcs;
    }

    String[] arr = str.split( "," );
    int count = 0;
    for (String createrid : arr) {
        requestMainMap.put( "cglxmc", xmmc );//项目名称
        requestMainMap.put( "sqr", userid );//申请人
        requestMainMap.put( "tsr", createrid );//推送人
        requestMainMap.put( "lc", lc );//项目名称
        requestMainMap.put( "sqrq", DateUtil.getCurrentDate() );//申请日期
        requestMainMap.put( "xxlj", "<a href=" + link + " target='_blank'>查看</a>" );//项目编号
        bb.writeLog( "requestMainMap=" + requestMainMap.toString() );
        String requestid = createHandler.WorkflowCreateByRequestMap( createrid, workflowId, requestName, IsNextFlow, requestMainMap );
        if (Integer.parseInt( requestid ) > 0) {
            count++;
        }
    }
    bb.writeLog( "counts=" + arr.length + ",count=" + count );
    if ((arr.length>0 && count>0) &&(arr.length == count)) {
        jsonData.put( "flag", true );
    } else {
        jsonData.put( "flag", false );
    }
    out.print( jsonData.toJSONString() );
    bb.writeLog( "getNewsAlter_Ajax=" + jsonData.toJSONString() );
%>
<%!
    public static String getUserid(String gysmc) {
        RecordSet rs = new RecordSet();
        String sql = "select wm_concat(t2.id) from uf_srm_gyskpx t1 left join hrmresource t2 on t1.gyszh=t2.loginid where t1.id in (" + gysmc + ")";
        rs.executeQuery( sql );
        rs.next();
        String ids = Util.null2String( rs.getString( 1 ) );
        return ids;
    }
    public static String equestStr(String str) {
        String [] arr = str.split(",");
        StringBuilder sb = new StringBuilder();
        List<String> list = new ArrayList<>();
        for (int i=0; i<arr.length; i++) {
            if(!list.contains(arr[i])) {
                list.add(arr[i]);
                sb.append( arr[i] + "," );
            }
        }
        String result = sb.substring( 0, sb.length() - 1 );
        return result;
    }
%>
